package com.ego.service.impl;

import com.ego.commons.utils.FastDFSClient;
import com.ego.service.PicService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class PicServiceImpl implements PicService {

    @Value("${ego.fastdfs.nginx}")
    private String nginxHost;
    @Override
    public Map<String, Object> upload(MultipartFile file) {
        Map<String,Object> map = new HashMap<>();
        try {
            String[] results = FastDFSClient.uploadFile(file.getInputStream(), file.getOriginalFilename());
            map.put("error",0);
            // 测试环境和上线环境不一致的情况：通过软编码解决
            map.put("url",nginxHost+results[0]+"/"+results[1]);
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("error",1);
        map.put("message","错误信息");
        return map;
    }
}
